﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Calendar;
using Cn.Cellula.Biz;
using System.Drawing;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Calendar {
    public partial class TodoView : PageSupport {
        String eoKey = null;
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        DateTime CurDt = DateTime.Now;
        protected void Page_Load(object sender, EventArgs e) {
            eoKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                FillForm();
            }
        }
        private void FillForm() {
            if (!String.IsNullOrEmpty(eoKey)) {
                VCalendarTodoEO eo = biz.LoadCalendarTodo(this.OwnerKey,null, eoKey);
                if (eo == null) {
                    this.btn_Finish.Visible = false;
                    this.btn_Update.Visible = false;
                    return;
                }
                this.btn_Update.Visible = (eo.Operator == this.Actor.ID);
                this.btn_Finish.Visible = (eo.IfClose != 1);
                this.lab_OperatorName.Text = eo.OperatorName;
                this.lab_AssignTo.Text = eo.AssignToUserName;
                this.lab_content.Text = eo.Content;
                this.lab_EndToPlan.Text = (eo.EndToPlan==null)?"":Convert.ToDateTime(eo.EndToPlan).ToString("yyyy-MM-dd HH:mm:ss");
                this.lab_EndToReal.Text = (eo.EndToReal == null) ? "" : Convert.ToDateTime(eo.EndToReal).ToString("yyyy-MM-dd HH:mm:ss");
                this.lab_LevelName.Text = eo.LevelName;
                if (eo.TodoLevel != CodeTypeDef.Todolevel.Normal) {
                    this.lab_LevelName.ForeColor = Color.Red;
                }
                this.lab_OccurDt.Text = eo.OccurDt.ToString("yyyy-MM-dd");
                this.lab_Members.Text = eo.MembersName;
                this.lab_Private.Text = (eo.IsPrivate == 1) ? "是" : "否";
                if (eo.IsPrivate == 1)
                    this.lab_Private.ForeColor = Color.Red;
                this.lab_StartToPlan.Text = (eo.StartToPlan == null) ? "" : Convert.ToDateTime(eo.StartToPlan).ToString("yyyy-MM-dd HH:mm:ss");
                this.lab_StartToReal.Text = (eo.StartToReal == null) ? "" : Convert.ToDateTime(eo.StartToReal).ToString("yyyy-MM-dd HH:mm:ss");
                this.lab_TodoName.Text = this.lab_TodoName2.Text = eo.TodoTitle;
                this.lab_Type.Text = eo.TypeName;

                this.btn_Finish.Enabled = this.btn_Update.Enabled = (eo.IsLocked == 0);

                this.lab_IsLock.Text = (eo.IsLocked == 1) ? "是" : "否";

                if (String.IsNullOrEmpty(eo.RefItemName)) {
                    this.hl_RefItem.Text = TxtDef.Item.NoData;
                    this.hl_RefItem.Enabled = false;
                } else {
                    this.hl_RefItem.Text = eo.RefItemName;
                    this.hl_RefItem.NavigateUrl = "Admin/Item/ItemView.aspx?id=" + eo.RefItemKey;
                }
                this.lab_CostHours.Text = eo.CostHours+"";
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e) {
            Response.Redirect("EditToDo.aspx?id=" + eoKey);
        }

        protected void btn_Finish_Click(object sender, EventArgs e) {
            TCalendarTodoEO eo = biz.Load<TCalendarTodoEO>(eoKey);
            eo.EndToReal = DateTime.Now;
            eo.IfClose = 1;
            biz.Update<TCalendarTodoEO>(eo);
            Response.Redirect("TodoView.aspx?id=" + eoKey);
        }
    }
}